package com.nova.edc.service.energy.impl;

import java.util.List;

import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.service.system.AreaPointService;
import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.energy.CollectDataDao;
import com.nova.edc.model.energy.CollectData;
import com.nova.edc.model.energy.CollectDataDetail;
import com.nova.edc.model.energy.Summary;
import com.nova.edc.service.energy.CollectDataService;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.parsing.SQLParser;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.sql.SqlBuffer;

public class CollectDataServiceImpl implements CollectDataService {

    @Autowired
    private CollectDataDao novaCollectDataDao;

    @Autowired
    private AreaPointService areaPointService;

    @Override
    public List<CollectData> findDataSummary(int companyCode, String startTime,
                                             String endTime) {
        String sql = "select cd.GatewayID,cd.SensorID,cd.ChannelID,count(1) useminute from t_collectdata cd"
                + " where cd.CompanyCode = ?"
                + " and cd.ActiveState = 1"
                + " and cd.ActiveTime > ?"
                + " and cd.ActiveTime < ?"
                + " group by cd.GatewayID,cd.SensorID,cd.ChannelID";
        List<CollectData> result = JdbcUtils.query(CollectData.class, sql,
                companyCode, startTime, endTime);
        return result;
    }

    @Override
    public void wrapCollectData(CollectData collectData) {
        // 更新数据到companycode，从areapoint
        AreaPoint areaPoint = new AreaPoint();
        areaPoint.setGatewayid(collectData.getGatewayid());
        areaPoint.setChannelid(collectData.getChannelid());
        areaPoint.setSensorid(collectData.getSensorid());
        // 更新公司编码
        collectData.setCompanycode(areaPointService.getCompanyCode(areaPoint));
        // 更新采集数据的节点、区域信息
        wrapPoint(collectData);
    }


    @Override
    public void wrapCollectData(List<CollectData> collectDatas) {
        for (CollectData collectData : collectDatas) {
            wrapCollectData(collectData);
        }
    }

    @Override
    public void wrapPoint(CollectData collectData) {
        AreaPoint areaPoint = areaPointService.getByDetail(collectData.getGatewayid(), collectData.getSensorid(), collectData.getChannelid());
        if(areaPoint != null) {
            collectData.setPointid(areaPoint.getId());
            collectData.setAreaid(areaPoint.getAreaid());
        }
    }

    @Override
    public void insertAll(List<CollectData> collectDatas) {
        novaCollectDataDao.insert(collectDatas);
    }

    @Override
    public List<Summary> summaryCollectData(String gateway, String startTime,
                                            String endTime) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("collectDataSQL.summaryCollectData"), true);
        List<Summary> result = JdbcUtils.query(Summary.class,
                sqlBuffer.getSelectSql(), gateway, startTime, endTime);
        return result;
    }


    @Override
    public List<CollectData> findDataDetail(String gatewayid, Integer sensorid,
                                            Integer channelid, String startTime, String endTime,
                                            Integer activeState) {
        Query<CollectData> query = novaCollectDataDao.query();
        query.add(Restrictions.eq("t_collectdata.GatewayID", gatewayid));
        query.add(Restrictions.eq("t_collectdata.SensorID", sensorid));
        query.add(Restrictions.eq("t_collectdata.ChannelID", channelid));
        query.add(Restrictions.eq("t_collectdata.ActiveState", activeState));
        query.add(Restrictions.between("t_collectdata.ActiveTime", startTime,
                endTime));
        return query.list();
    }

    @Override
    public List<CollectDataDetail> findEveryHour(String gatewayid,
                                                 Integer sensorid, Integer channelid, String startTime,
                                                 String endTime) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("collectDataSQL.findEveryHour"), true);
        List<CollectDataDetail> collectDataDetails = JdbcUtils.query(
                CollectDataDetail.class, sqlBuffer.getSql(), gatewayid,
                sensorid, channelid, startTime, endTime);
        return collectDataDetails;
    }
}
